var mydialog;

function c_search(){
	mydialog=art.dialog({
		title:"處理中",
		lock:true
	});
	var datas=new Array();
	var datas_id=new Array();
	var birth1=$("#cu_birthday").val();
	var birth2=$("#cu_birthday2").val();
	if(birth1!=''&&birth2!=''){
		$("#birth").val(birth1+','+birth2);
	}else{
		$("#birth").val('');
	}
	var data=$(".post").get();
	var i=0;
	$.each(data,function(key,value){
		if(value["type"]==='radio'){
			if(value["checked"]){
				datas[i]=$(this).val();
				datas_id[i]=value["id"];
			}
		}else{
			if($(this).val()!=''&&$(this).val()!=undefined){
				datas[i]=$(this).val();
				datas_id[i]=value["id"];
			}
		}
		i++;
	});
	
	$.ajax({
		url:'userinfo_search',
		type:'post',
		dataType:'json',
		data:{s_c:datas,operator:'2',ids:datas_id,type:'22'},
		success:c_search_result
	});
}

function showSelInfo(table,field,obj){
	mydialog = art.dialog({
		title:'選擇',
		width:300,
		height:210,
		lock:true,
		ok:function(){
			$('#'+obj).val();
		}
	});
	$.ajax({
		url:'showSelInfo',
		type:'post',
		dataType:'html',
		data:{table:table,field:field},
		success:function(data){
			mydialog.content(data);
		}
	});
}

var header=new Array("客戶姓名","【配對】","公司","行動電話","住家電話","公司電話","傳真","重要性","住家地址","公司地址","經紀人");

function c_search_result(data){
	if(data.status===1){
		mydialog.content("非法訪問，不能查詢").title("提示");
	}else if(data.status===0){
		mydialog.content("沒有權限，不能查詢").title("提示");
	}else if(data.status===3){
		mydialog.close();
		$("#result").html("<center>暫無查詢結果</center>");
		//$("#result").html(data.data);
	}else if(data.status===2){
		mydialog.close();
		var table="<table id='results'></table>";
		$("#result").html(table);
		$("#result #results").append("<thead><tr><th>操作</th><th>姓名</th><th>公司</th><th>行動電話</th><th>住家電話</th><th>公司電話</th><th>傳真</th><th>重要性</th><th>住家地址</th><th>公司地址</th><th>經紀人</th><th></th></tr></thead>");
		$("#result #results").append("<tbody>");
		$.each(data.data,function(key,value){
			var cu_impor=value["cu_impor"];
			if(cu_impor==null){
				cu_impor="";
			}
			$("#result #results").append("<tr><td><input type='button' value='操作' onclick=S_operator("+value["id"]+",'"+value["cu_name"]+"'); /></td><td>"+value["cu_name"]+"</td><td>"+value["cu_company"]+"</td><td>"+value["cu_mobile"]+"</td><td>"+value["cu_htel"]+"</td><td>"+value["cu_ctel"]+"</td><td>"+value["cu_fax"]+"</td><td>"+cu_impor+"</td><td>"+value["cu_hadd"]+"</td><td>"+value["cu_cadd"]+"</td><td>"+value["cu_broker1"]+"</td></tr>");
		});
		$("#result #results").append("</tbody>");
		$("#result").append("<script type='text/javascript'>$('#results th').attr('width','80');</script>");
		$("#result").append("<link rel='stylesheet' type='text/css' href='../../public/css/flex.css' />");
		$("#result").append("<link rel='stylesheet' type='text/css' href='../../public/css/flexigrid.pack.css' />");
		$("#result").append("<script type='text/javascript' src='../../public/js/flexigrid.pack.js'></script>");
		$("#result").append("<script type='text/javascript' src='../../public/js/flextable.js'></script>");
		//$("#result").html(data.info);
	}
}

var id=0;
var table='zb050h';

function S_operator(id,name){
	mydialog=art.dialog({
		title:"操作-"+name,
		content:"<a class='operator_alter' onclick='S_alter("+id+");'><i class='icon-edit'></i>修改</a>&nbsp;&nbsp;<a class='operator_del' onclick='S_delete("+id+");' ><i class='icon-trash'></i>刪除</a>",
		lock:true
	});
}

function S_alter(id){
	//location.href="userinfo_alter?operator=23&&id="+id;
	window.open("userinfo_alter?operator=23&&id="+id,"_blank");
}

function S_delete(id){
	if(confirm("你确定删除吗？")){
		mydialog.content("...").title('处理中...');
		$.ajax({
			url:'del_action',
			type:'post',
			dataType:'json',
			data:{table:'zb050h',id:id,operator:'24'},
			success:function(data){
				if(data.status===3){
					mydialog.close();
					art.dialog({
						title:'提示',
						content:'刪除成功',
						lock:true,
						ok:function(){
							//c_search();
							location.reload();
						}
					});
				}else{
					mydialog.close();
					art.dialog({
						title:'提示',
						content:'刪除失敗',
						lock:true
					});
				}
			}
		});
	}
}